/*==manifest.json==
    	{
    		"name": "饭团影视",
    		"version": 1.0,
    		"home":"https://www.fantuan.run",
    		"matches": "/player",
    		"update": "https://gitee.com/joinli/solo/raw/master/extension/extension_fantuan.js",
    		"author":"JoinLi",
    		"ua":"Mobile"
    	}
    */

//页面开始加载
function onPageStarted() {

}

//页面加载完成
function onPageFinished() {
	getPlayData();
}


/*
发送给Solo的json数据格式 event是事件名称，data是构造的数据
{
  "event": "playData",
  "data": {
    "title": "视频名称",
    "list": [
      {
        "title": "视频源1",
        "play_list": [
          {
            "name": "HD中字",
            "url": "https: //test/player/93555-1-1.html"
          }
        ]
      },
      {
        "title": "视频源2",
        "play_list": [
          {
            "name": "HD英文",
            "url": "https: //test/player/93555-2-1.html"
          }
        ]
      }
    ]
  }
}

*/

function getPlayData() {
	const panels = document.querySelectorAll('.module-play-list');
	//视频名称
	const title = document.title.split(' ')[0].replace("在线播放", "");
	const result = {
		title: title,
		list: []
	};
	panels.forEach((panel, index) => {
		const panelData = {
			title: '视频源' + (index + 1).toString(), // 标题为1, 2, 3...
			play_list: []
		};

		// 获取当前panel中的所有链接
		const links = panel.querySelectorAll('.module-play-list-link');
		links.forEach(link => {
			const name = link.querySelector('span').textContent;
			const href = link.getAttribute('href');
			const url = window.location.origin + href;
			panelData.play_list.push({
				name,
				url
			});

		});
		// 将当前panel的数据添加到结果中
		result.list.push(panelData);
	});
	const data = JSON.stringify({
		event: 'playData',
		data: result
	});
	//发送构造的json数据给Solo
	if (window.dartJavascriptChannel) {
		window.dartJavascriptChannel.postMessage(data);
	}
	//如果网页视频不支持自动播放，浏览器则无法嗅探到资源，这时候则需要模拟点击屏幕坐标，实现播放事件
	const element = document.elementFromPoint(200, 200);
	if (element.tagName.toLowerCase() === 'iframe') {
		const iframeDoc = element.contentDocument || element.contentWindow.document;
		iframeDoc.elementFromPoint(200, 200).click();
	}

}